<?php

namespace App\V3\V3_2\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ProductOrderModel extends Model
{
    use HasFactory;

    protected $table   = 'product_order';
    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储
    //toArray 时间转换问题
    public static function detail($id)
    {
        return self::with(['slaveInfo' => function($query) {
            $query->select(
                'product_slave.id',
                'num',
                'finished_num',
                'product_slave.is_product_materiel',
                'type_name',
                'materiel_name',
                'product_order_id',
                'a.id as type_id',
                'a.materiel_img',
                'a.materiel_unit',
                'a.materiel_no',
                'a.selling_price',
                'a.is_use_serial',
                'storage_operate_num'
            )
                ->leftJoin('materiel_type as a', 'a.id','=','type_id');
        }])
//            ->with(['slaveContent' => function($query) {
//                $query->select(
//                    'product_slave_content.id',
//                    'product_slave_content.product_order_id',
//                    'a.id as type_id',
//                    'a.type_name',
//                    'a.materiel_name',
//                    'product_slave_content.product_source',
//                    'product_slave_content.product_source_id',
//                    'product_slave_content.num',
//                )
//                    ->leftJoin('materiel_type as a', 'a.id', '=', 'product_slave_content.type_id');
//            }])
//            ->with('slaveContentStorage', function($query) {
//                $query->select(
//                    'product_slave_content_storage.id',
//                    'product_slave_content_storage.product_order_id',
//                    'product_slave_content_storage.type_id',
//                    'a.type_name',
//                    'a.materiel_name',
//                    'a.materiel_unit',
//                    'a.is_use_serial',
//                    'c.storage_name',
//                    'c.storage_address',
//                    'd.shelf_name',
//                    'e.location_name',
//                    'g.serial_no',
//                    'product_slave_content_storage.serial_id',
//                    'product_slave_content_storage.storage_manage_id',
//                    'product_slave_content_storage.storage_operate_num as rest_num'
//                )
//                    ->leftJoin('materiel_type as a', 'a.id', '=', 'product_slave_content_storage.type_id')
//                    ->leftJoin('storage_manage as b', 'b.id', '=', 'product_slave_content_storage.storage_manage_id')
//                    ->leftJoin('storage as c', 'c.id', '=', 'b.storage_id')
//                    ->leftJoin('storage_shelf as d', 'd.storage_id', '=', 'c.id')
//                    ->leftJoin('storage_shelf_location as e', 'e.shelf_id', '=', 'd.id')
//                    ->leftJoin('serial_number as g', 'g.id', '=', 'product_slave_content_storage.serial_id');
//            })
            ->where('id', $id)
//            ->where('main_id', $mainId)
            ->where('delete_flag', 0)
            ->first();
    }

    public static function detail1($id)
    {
        return self::with(['slaveInfo' => function($query) {
            $query->select(
                'product_slave.id',
                'num',
                'finished_num',
                'product_slave.is_product_materiel',
                'type_name',
                'materiel_name',
                'product_order_id',
                'a.id as type_id',
                'a.materiel_img',
                'a.materiel_unit',
                'a.materiel_no',
                'a.selling_price',
                'a.is_use_serial',
                'storage_operate_num'
            )
                ->leftJoin('materiel_type as a', 'a.id','=','type_id');
        }])
            ->with(['slaveContent' => function($query) {
                $query->select(
                    'product_slave_content.id',
                    'product_slave_content.product_order_id',
                    'a.id as type_id',
                    'a.type_name',
                    'a.materiel_name',
                    'product_slave_content.product_source',
                    'product_slave_content.product_source_id',
                    'product_slave_content.num',
                )
                    ->leftJoin('materiel_type as a', 'a.id', '=', 'product_slave_content.type_id');
            }])
            ->with('slaveContentStorage', function($query) {
                $query->select(
                    'product_slave_content_storage.id',
                    'product_slave_content_storage.product_order_id',
                    'product_slave_content_storage.type_id',
                    'a.type_name',
                    'a.materiel_name',
                    'a.materiel_unit',
                    'a.is_use_serial',
                    'c.storage_name',
                    'c.storage_address',
                    'd.shelf_name',
                    'e.location_name',
                    'g.serial_no',
                    'product_slave_content_storage.serial_id',
                    'product_slave_content_storage.storage_manage_id',
                    'product_slave_content_storage.num',
                    'product_slave_content_storage.storage_operate_num as rest_num'
                )
                    ->leftJoin('materiel_type as a', 'a.id', '=', 'product_slave_content_storage.type_id')
                    ->leftJoin('storage_manage as b', 'b.id', '=', 'product_slave_content_storage.storage_manage_id')
                    ->leftJoin('storage as c', 'c.id', '=', 'b.storage_id')
                    ->leftJoin('storage_shelf as d', 'd.storage_id', '=', 'c.id')
                    ->leftJoin('storage_shelf_location as e', 'e.shelf_id', '=', 'd.id')
                    ->leftJoin('serial_number as g', 'g.id', '=', 'product_slave_content_storage.serial_id');
            })
            ->where('id', $id)
//            ->where('main_id', $mainId)
            ->where('delete_flag', 0)
            ->first();
    }

    public static function searchByMaterielNameInSlave(array $data, array $condition, $keyword, $startTime, $endTime)
    {
        $query = self::select(
            'a.id'
        )
            ->from('product_order as a')
            ->leftJoin('product_slave as b', 'a.id', '=', 'b.product_order_id')
            ->leftJoin('materiel_type as c', 'c.id', '=', 'b.type_id')
            ->where($condition)
            ->where('a.delete_flag', 0);

            if(!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('a.created_at', [$startTime, $endTime]);
            }
            else if(!empty($startTime) && empty($endTime)) {
                $query->where('a.created_at', '>=', $startTime);
            }
            else if(empty($startTime) && !empty($endTime)) {
                $query->where('a.created_at', '<=', $endTime);
            }

            return $query->where(function($query) use ($keyword) {
                $query->where('c.materiel_name', 'LIKE', '%'.$keyword.'%');
            })
            ->get()->toArray();
    }

    public static function searchByMaterielNameInSlaveContent(array $data, array $condition, $keyword, $startTime, $endTime)
    {
        $query = self::select(
            'a.id'
        )
            ->from('product_order as a')
            ->leftJoin('product_slave_content as b', 'a.id', '=', 'b.product_order_id')
            ->leftJoin('materiel_type as c', 'c.id', '=', 'b.type_id')
            ->where($condition)
            ->where('a.delete_flag', 0);

            if(!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('a.created_at', [$startTime, $endTime]);
            }
            else if(!empty($startTime) && empty($endTime)) {
                $query->where('a.created_at', '>=', $startTime);
            }
            else if(empty($startTime) && !empty($endTime)) {
                $query->where('a.created_at', '<=', $endTime);
            }

            return $query->where(function($query) use ($keyword) {
                $query->where('c.materiel_name', 'LIKE', '%'.$keyword.'%');
            })
                ->get()->toArray();
    }

    public static function list(array $orderId, array $data)
    {
        return self::select(
            'product_order.id',
            'product_order.product_no',
            'product_order.product_status',
            'product_order.product_source',
            'product_order.storage_operate_status',
            'product_order.product_source_id',
            'product_order.total_num',
            'product_order.finished_num',
            'product_order.relation_no',
            'product_order.product_way',
            'nickname',
            'product_order.is_cancel'
        )
            ->with(['slaveInfo' => function($query) {
                $query->select(
                    'product_slave.id',
                    'num',
                    'product_slave.is_product_materiel',
                    'type_name',
                    'materiel_name',
                    'product_order_id',
                    'a.materiel_unit'
                )
                    ->leftJoin('materiel_type as a', 'a.id','=','type_id')
                    ->with('slaveContent',function($query) {
                        $query->select('type_id');
                    });
            }])
//            ->with(['slaveContent' => function($query) {
//                $query->select(
//                    'product_slave_content.id',
//                    'product_slave_content.product_order_id',
//                    'a.type_name',
//                    'a.materiel_name',
//                    'a.is_use_serial',
//                    'product_slave_content.num',
//                    'a.materiel_unit'
//                )
//                    ->leftJoin('materiel_type as a', 'a.id', '=', 'product_slave_content.type_id')
//                    ->with('slaveContentStorage', function($query) {
//                        $query->select(
//                            'product_slave_content_storage.id',
//                            'product_slave_content_storage.product_order_id',
//                            'a.type_name',
//                            'a.materiel_name',
//                            'c.storage_name',
//                            'c.storage_address',
//                            'd.shelf_name',
//                            'e.location_name',
//                            'g.serial_no',
//                            'product_slave_content_id'
//                        )
//                            ->leftJoin('materiel_type as a', 'a.id', '=', 'product_slave_content_storage.type_id')
//                            ->leftJoin('storage_manage as b', 'b.id', '=', 'product_slave_content_storage.storage_manage_id')
//                            ->leftJoin('storage as c', 'c.id', '=', 'b.storage_id')
//                            ->leftJoin('storage_shelf as d', 'd.storage_id', '=', 'c.id')
//                            ->leftJoin('storage_shelf_location as e', 'e.shelf_id', '=', 'd.id')
//                            ->leftJoin('serial_number as g', 'g.id', '=', 'product_slave_content_storage.serial_id');
//                            });
//            }])
                ->leftJoin('admin', 'product_order.user_id', '=', 'admin.id')
                ->where('product_order.delete_flag', 0)
                ->whereIn('product_order.id', $orderId)
                ->orderBy('product_order.id', 'desc')
            ->paginate($data['pageSize'],['*'], '',$data['currentPage']);
    }

    public static function searchByTypeNameInSlave(array $data, array $condition, $keyword, $startTime, $endTime)
    {
        $query = self::select(
            'a.id'
        )
            ->from('product_order as a')
            ->leftJoin('product_slave as b', 'a.id', '=', 'b.product_order_id')
            ->leftJoin('materiel_type as c', 'c.id', '=', 'b.type_id')
            ->where($condition)
            ->where('a.delete_flag', 0);

        if(!empty($startTime) && !empty($endTime)) {
            $query->whereBetween('created_at', [$startTime, $endTime]);
        }
        else if(!empty($startTime) && empty($endTime)) {
            $query->where('created_at', '>=', $startTime);
        }
        else if(empty($startTime) && !empty($endTime)) {
            $query->where('created_at', '<=', $endTime);
        }

        return $query->where(function($query) use ($keyword) {
            $query->where('c.type_name', 'LIKE', '%'.$keyword.'%');
        })
            ->get()->toArray();
    }

    public static function searchByTypeNameInSlaveContent(array $data, array $condition, $keyword, $startTime, $endTime)
    {
        $query = self::select(
            'a.id'
        )
            ->from('product_order as a')
            ->leftJoin('product_slave_content as b', 'a.id', '=', 'b.product_order_id')
            ->leftJoin('materiel_type as c', 'c.id', '=', 'b.type_id')
            ->where($condition)
            ->where('a.delete_flag', 0);

        if(!empty($startTime) && !empty($endTime)) {
            $query->whereBetween('created_at', [$startTime, $endTime]);
        }
        else if(!empty($startTime) && empty($endTime)) {
            $query->where('created_at', '>=', $startTime);
        }
        else if(empty($startTime) && !empty($endTime)) {
            $query->where('created_at', '<=', $endTime);
        }

        return $query->where(function($query) use ($keyword) {
            $query->where('c.type_name', 'LIKE', '%'.$keyword.'%');
        })
            ->get()->toArray();
    }

    public static function searchByMaterielNoInSlave(array $data, array $condition, $keyword, $startTime, $endTime)
    {
        $query = self::select(
            'a.id'
        )
            ->from('product_order as a')
            ->leftJoin('product_slave as b', 'a.id', '=', 'b.product_order_id')
            ->leftJoin('materiel_type as c', 'c.id', '=', 'b.type_id')
            ->where($condition)
            ->where('a.delete_flag', 0);

        if(!empty($startTime) && !empty($endTime)) {
            $query->whereBetween('created_at', [$startTime, $endTime]);
        }
        else if(!empty($startTime) && empty($endTime)) {
            $query->where('created_at', '>=', $startTime);
        }
        else if(empty($startTime) && !empty($endTime)) {
            $query->where('created_at', '<=', $endTime);
        }

        return $query->where(function($query) use ($keyword) {
            $query->where('c.materiel_no', 'LIKE', '%'.$keyword.'%');
        })
            ->get()->toArray();
    }

    public static function searchByMaterielNoInSlaveContent(array $data, array $condition, $keyword, $startTime, $endTime)
    {
        $query = self::select(
            'a.id'
        )
            ->from('product_order as a')
            ->leftJoin('product_slave_content as b', 'a.id', '=', 'b.product_order_id')
            ->leftJoin('materiel_type as c', 'c.id', '=', 'b.type_id')
            ->where($condition)
            ->where('a.delete_flag', 0);

        if(!empty($startTime) && !empty($endTime)) {
            $query->whereBetween('created_at', [$startTime, $endTime]);
        }
        else if(!empty($startTime) && empty($endTime)) {
            $query->where('created_at', '>=', $startTime);
        }
        else if(empty($startTime) && !empty($endTime)) {
            $query->where('created_at', '<=', $endTime);
        }

        return $query->where(function($query) use ($keyword) {
            $query->where('c.materiel_no', 'LIKE', '%'.$keyword.'%');
        })
            ->get()->toArray();
    }

    public static function searchByProductNoInOrder(array $data, array $condition, $keyword, $startTime, $endTime)
    {
        $query = self::select(
            'id'
        )
            ->from('product_order as a')
            ->where($condition)
            ->where('a.delete_flag', 0);

        if(!empty($startTime) && !empty($endTime)) {
            $query->whereBetween('created_at', [$startTime, $endTime]);
        }
        else if(!empty($startTime) && empty($endTime)) {
            $query->where('created_at', '>=', $startTime);
        }
        else if(empty($startTime) && !empty($endTime)) {
            $query->where('created_at', '<=', $endTime);
        }

        return $query->where(function($query) use ($keyword) {
            $query->where('product_no', 'LIKE', '%'.$keyword.'%');
        })
            ->get()->toArray();
    }

    public static function searchByRelationNoInOrder(array $data, array $condition, $keyword, $startTime, $endTime)
    {
        $query = self::select(
            'id'
        )
            ->from('product_order as a')
            ->where($condition)
            ->where('a.delete_flag', 0);

        if(!empty($startTime) && !empty($endTime)) {
            $query->whereBetween('created_at', [$startTime, $endTime]);
        }
        else if(!empty($startTime) && empty($endTime)) {
            $query->where('created_at', '>=', $startTime);
        }
        else if(empty($startTime) && !empty($endTime)) {
            $query->where('created_at', '<=', $endTime);
        }

        return $query->where(function($query) use ($keyword) {
            $query->where('relation_no', 'LIKE', '%'.$keyword.'%');
        })
            ->get()->toArray();
    }

    public static function searchByOperatorInOrder(array $data, array $condition, $keyword, $startTime, $endTime)
    {
        $query = self::select(
            'a.id'
        )
            ->from('product_order as a')
            ->leftJoin('admin', 'admin.id', '=', 'user_id')
            ->where($condition)
            ->where('a.delete_flag', 0);

        if(!empty($startTime) && !empty($endTime)) {
            $query->whereBetween('created_at', [$startTime, $endTime]);
        }
        else if(!empty($startTime) && empty($endTime)) {
            $query->where('created_at', '>=', $startTime);
        }
        else if(empty($startTime) && !empty($endTime)) {
            $query->where('created_at', '<=', $endTime);
        }

        return $query->where(function($query) use ($keyword) {
            $query->where('nickname', 'LIKE', '%'.$keyword.'%');
        })
            ->get()->toArray();
    }

    public static function searchAllWithOutKeyword(array $data, array $condition, $startTime, $endTime)
    {
        $query = self::select(
            'a.id'
        )
            ->from('product_order as a')
            ->where($condition)
            ->where('a.delete_flag', 0);

        if(!empty($startTime) && !empty($endTime)) {
            $query->whereBetween('created_at', [$startTime, $endTime]);
        }
        else if(!empty($startTime) && empty($endTime)) {
            $query->where('created_at', '>=', $startTime);
        }
        else if(empty($startTime) && !empty($endTime)) {
            $query->where('created_at', '<=', $endTime);
        }

        return $query->get()->toArray();
    }

    public static function getMainOrderInfo($id)
    {
        return self::where('id', $id)->first();
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function slaveInfo()
    {
        return self::hasMany(ProductSlaveModel::class, 'product_order_id', 'id');
    }

    public function slaveContent()
    {
        return self::hasMany(ProductSlaveContentModel::class, 'type_id', 'id');
    }

    public function slaveContentStorage()
    {
        return self::hasMany(ProductSlaveContentStorageModel::class, 'product_order_id', 'id');
    }
   static public function getTodayProductOrderCount($condition, $type,$startTime, $endTime)
    {
         $query = self::where($condition)
            ->where('delete_flag',0);
             if(!empty($type)){
                 if($type==2){
                 $query->where('storage_operate_status',2);
                 }else{
                     $query->where('storage_operate_status','!=',2);
                 }
             }
            return $query->whereBetween('created_at', [$startTime, $endTime])
            ->count();
    }

}
